﻿using GuanYijia.Application.IntPut;
using GuanYijia.Application.OutPut;
using Newtonsoft.Json;
using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GuanYijia.Application.ProductionManagement
{
    public class SendNew
    {
        public static async Task SimpleSendMsgAsync(AddProductionTaskHeadDto productiondto)
        {
            string queueName = "erp对接"; // 队列名

            try
            {
                using (var connection = await RabbitMQHelperNew.GetConnectionAsync())
                {
                    using (var channel = await connection.CreateChannelAsync())
                    {
                        // 声明队列
                        await channel.QueueDeclareAsync(queueName, durable: false, exclusive: false, autoDelete: false, arguments: null);


                        string message = JsonConvert.SerializeObject(productiondto);
                            var body = Encoding.UTF8.GetBytes(message); // 消息体

                            // 创建基本属性对象
                            var basicProperties = new BasicProperties(); // 直接使用 BasicProperties 创建

                            // 发送消息
                            await channel.BasicPublishAsync(
                                exchange: "",
                                routingKey: queueName,
                                mandatory: false,
                                basicProperties: basicProperties, // 使用基本属性对象
                                body: body);

                            Console.WriteLine($"发送消息到队列:{queueName}, 内容:{message}");
                        
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"发生错误: {ex.Message}");
            }
        }
    }
}
